#ifndef TIME_HPP
#define TIME_HPP

#include <chrono>

namespace sh
{

typedef std::chrono::high_resolution_clock clock;

typedef std::chrono::hours hours;

typedef std::chrono::minutes minutes;

typedef std::chrono::seconds seconds;

typedef std::chrono::milliseconds milliseconds;

typedef std::chrono::microseconds microseconds;

/**
 * 基于stl::chrono的计时器
 *
 * @author: sherlock_lht
 */
class Time
{
public:
    Time() = default;

    /**
     * Sets this time to the current time.
     */
    void start()
    {
        _begin = clock::now();
    }

    /**
     * Sets this time to the current time and returns the number of milliseconds
     * that have elapsed since the last time start() or restart() was called.
     */
    long long restart()
    {
        long long elapsed = std::chrono::duration_cast<milliseconds>(clock::now() - _begin).count();
        _begin = clock::now();
        return elapsed;
    }

    /**
     * Returns the number of milliseconds that have elapsed since
     * the last time start() or restart() was called.
     */
    long long elapsed() const
    {
        return std::chrono::duration_cast<milliseconds>(clock::now() - _begin).count();
    }

    /**
     * Returns the number of seconds that have elapsed since
     * the last time start() or restart() was called.
     */
    long long elapsedSecond() const
    {
        return std::chrono::duration_cast<seconds>(clock::now() - _begin).count();
    }

    /**
     * Returns the number of microseconds that have elapsed since
     * the last time start() or restart() was called.
     */
    long long elapsedMicroseconds() const
    {
        return std::chrono::duration_cast<microseconds>(clock::now() - _begin).count();
    }

    /**
     * Returns the number of minutes that have elapsed since
     * the last time start() or restart() was called.
     */
    long long elapsedMinutes() const
    {
        return std::chrono::duration_cast<minutes>(clock::now() - _begin).count();
    }

    /**
     * Returns the number of hours that have elapsed since
     * the last time start() or restart() was called.
     */
    long long elapsedHours() const
    {
        return std::chrono::duration_cast<hours>(clock::now() - _begin).count();
    }

private:
    std::chrono::time_point<clock> _begin;
};
}

#endif //TIME_HPP
